import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import remove from '@dfeidao/atom-web/local/remove';
import set from '@dfeidao/atom-web/local/set';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import refresh from '@dfeidao/atom-web/url/refresh';

export default async function a005(fd: IFeidaoAiBrowserComponent) {
	let i = get(fd, 'i');
	if (i === undefined || i > 0) {
		i = 1;
		// 获取项目数据
		const project = fd.data.node.querySelector<HTMLSelectElement>('[data-id="project"]');
		const project_no = project.value;
		const project_name = project.selectedOptions[0].text;
		// 提取系统参数
		const equip_depart_name = sessionStorage.getItem('www');
		const equip_depart_no = sessionStorage.getItem('ddd');
		const productid = "kinth_cloud_100";
		const responsible_no = sessionStorage.getItem('sss');
		const responsible = sessionStorage.getItem('aaa');
		// 获取项目资产编号
		const project_asset = await nodejs<{ result: string }>('project-assets/zj-000003/s002', {});
		const project_asset_no = project_asset.result;
		// 获取资产名称
		const assetsname = fd.data.node.querySelector<HTMLInputElement>('[data-id="assetsname"]').value;
		// 获取资产类型
		const asset_classification = fd.data.node.querySelector<HTMLSelectElement>('[data-id="asset_classification"]');
		const asset_classification_no = asset_classification.value;
		const asset_classification_name = asset_classification.selectedOptions[0].text;
		// 获取资产等级
		const asset_grade = fd.data.node.querySelector<HTMLSelectElement>('[data-id="asset_grade"]').value;
		// 获取品牌
		const brand_no = fd.data.node.querySelector<HTMLInputElement>('[data-id="brand_no"]').value;
		// 获取是否客户资产
		// tslint:disable-next-line:radix
		let customer_assets = Number.parseInt(fd.data.node.querySelector<HTMLSelectElement>('[data-id="customer_assets"]').value);
		// tslint:disable-next-line:use-isnan
		if (customer_assets === NaN) {
			customer_assets = 1;
		}
		// 获取单价
		const unitprice = fd.data.node.querySelector<HTMLInputElement>('[data-id="unitprice"]').value;
		const reg = new RegExp("^[0-9]*$");
		if (!reg.test(unitprice)) {
			error("请输入数字!");
			return;
		}
		// 获取资产位置
		const place = fd.data.node.querySelector<HTMLInputElement>('[data-id="address"]').value;
		// 获取资产维修时长
		const maintenance_time = fd.data.node.querySelector<HTMLInputElement>('[data-id="maintenance_time"]').value;
		if (!reg.test(maintenance_time)) {
			error("请输入数字!");
			return;
		}
		// 获取是否超时提醒
		// tslint:disable-next-line:radix
		let timeout_alert = Number.parseInt(fd.data.node.querySelector<HTMLSelectElement>('[data-id="timeout_alert"]').value);
		// tslint:disable-next-line:use-isnan
		if (timeout_alert === NaN) {
			timeout_alert = 1;
		}
		// 获取维护说明
		const remark = fd.data.node.querySelector<HTMLTextAreaElement>('[data-id="remark"]').value;
		// 获取
		const timeout_reminder_time = fd.data.node.querySelector<HTMLInputElement>('[data-id="timeout_reminder_time"]').value;
		if (!reg.test(timeout_reminder_time)) {
			error("请输入数字!");
			return;
		}
		// 判断信息是否完善
		if (asset_classification_no === "" || place === "" || assetsname === "" || maintenance_time === "" || project_no === "" || unitprice === "" || asset_grade === "") {
			error("请完善信息!");
			return;
		}
		// 添加数据
		const res = await nodejs<{
			code: 0 | 1;
			result: string;
		}>('project-assets/zj-000003/s003', {
			filter: {
				project_name,
				project_no,
				equip_depart_name,
				equip_depart_no,
				productid,
				responsible_no,
				responsible,
				project_asset_no,
				assetsname,
				asset_classification_no,
				asset_classification_name,
				asset_grade,
				brand_no,
				customer_assets,
				unitprice,
				place,
				maintenance_time,
				timeout_alert,
				remark,
				timeout_reminder_time,
				sec_image: ""
			}
		});
		// 判定是否添加成功
		if (res.code === 1) {
			success("添加成功");
			set(fd, 'i', i);
			remove(fd, 'i');
			refresh();
		} else {
			error("添加失败");
		}


	}
}
